import { Controller } from "../../mvc/core/Controller";
import { IMergeRechargeController } from "./interfaces/IMergeRechargeController";
import { MergeRechargeIcon } from "../view/consumeCount/MergeRechargeIcon";
import { IView } from "../../mvc/interfaces/IView";
import { ConsumeCountGiftBagModule } from "../../../modules/ConsumeCountGiftBagModule";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { MergeRechargeData } from "../view/consumeCount/MergeRechargeData";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { ClockManager } from "../manager/ClockManager";
import { GameProxy } from "../mvc/GameProxy";
import { TOpenRechargeGiftBag } from "../../../Message/Db/Tables/TOpenRechargeGiftBag";
import { IConsumeCountGiftBagModule } from "../../../modules/interfaces/IConsumeCountGiftBagModule";
type int = number;
//class MergeRechargeController
    
    export  class MergeRechargeController extends Controller implements IMergeRechargeController
    {
       

        public get mergeRechargeIcon(): MergeRechargeIcon
        {
            if (this._mergeRechargeIcon == null) 
            {
                this._mergeRechargeIcon = new MergeRechargeIcon();
                this._mergeRechargeIcon.updateIconInfo();
            }
            return this._mergeRechargeIcon;
        }

        protected /* override */ initView(): IView
        {
            if (this._mergeRecharge == null) 
            {
                this._mergeRecharge = new ConsumeCountGiftBagModule();
                this._mergeRecharge.type = GModuleEvent.Type_MergeRecharge;
            }
            return this._mergeRecharge;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ActiveMergeRechargeGiftBag, this.onActiveMergeRechargeGiftBag);
            Dispatcher.addEventListener(EventName.MergeRechargeIconClick, this.onMergeRechargeIconClickHandler);
            Dispatcher.addEventListener(EventName.MergeRechargeValue, this.onMergeRechargeValueHandler);
            Dispatcher.addEventListener(EventName.MergeRechargeHadGetGiftBag, this.onMergeRechargeHadGetGiftBagHandler);
            Dispatcher.addEventListener(EventName.MergeRechargeGiftBagGet, this.onMergeRechargeGiftBagGetHandler);
            Dispatcher.addEventListener(EventName.MergeRechargeItemSelected, this.onMergeRechargeItemSelectedHandler);
            Dispatcher.addEventListener(EventName.CurWorldLevelUpdate, this.onCurWorldLevelUpdateHandler);
            return;
        }

        /* internal  */onCurWorldLevelUpdateHandler(arg1: DataEvent): void
        {
            if (this._mergeRecharge) 
            {
                this._mergeRecharge.updateGiftBag();
                this._mergeRecharge.updateConsumeCountGold(cache.role.mergeRecharge);
            }
            return;
        }

        /* internal  */onActiveMergeRechargeGiftBag(arg1: SActiveToClient): void
        {
            var loc1=cache.active.isActiveInEffect(arg1.code);
            var loc2=MergeRechargeData.isHadGetAllGift(EActiveType._EActiveTypeMergeRechargeGiftBag);
            if (loc1 && !loc2) 
            {
                MergeRechargeData.activeToClient = arg1;
                ClockManager.instance.addDateCall(arg1.endDt, this.removeMergeRechargeActive);
                this.mergeRechargeIcon.show();
            }
            if (!this._isGetCodes) 
            {
                GameProxy.roleProxy.hadGetMergeRechargeGiftBag();
                this._isGetCodes = true;
            }
            return;
        }

        /* internal  */onMergeRechargeIconClickHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
                GameProxy.roleProxy.hadGetMergeRechargeGiftBag();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */removeMergeRechargeActive(): void
        {
            if (this._mergeRechargeIcon && !this._mergeRechargeIcon.isHide) 
            {
                this._mergeRechargeIcon.hide();
            }
            if (this._mergeRecharge && !this._mergeRecharge.isHide) 
            {
                view.hide();
            }
            if (MergeRechargeData.activeToClient) 
            {
                ClockManager.instance.removeDateCall(MergeRechargeData.activeToClient.endDt);
            }
            return;
        }

        /* internal  */onMergeRechargeValueHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._mergeRecharge) 
            {
                this._mergeRecharge.updateGiftBag();
                this._mergeRecharge.updateConsumeCountGold(loc1);
            }
            return;
        }

        /* internal  */onMergeRechargeHadGetGiftBagHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            MergeRechargeData.hadGetCodes = loc1;
            if (this._mergeRecharge) 
            {
                this._mergeRecharge.updateGiftBag();
                this._mergeRecharge.updateConsumeCountGold(cache.role.mergeRecharge);
            }
            this.isGetAllGift();
            return;
        }

        /* internal  */onMergeRechargeGiftBagGetHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getMergeRechargeGiftBag(loc1);
            return;
        }

        /* internal  */onMergeRechargeItemSelectedHandler(arg1: DataEvent): void
        {
            if (this._mergeRecharge) 
            {
                this._mergeRecharge.updateItemList(arg1.data as TOpenRechargeGiftBag);
            }
            return;
        }

        /* internal  */isGetAllGift(): void
        {
            var loc1=MergeRechargeData.isHadGetAllGift(EActiveType._EActiveTypeMergeRechargeGiftBag);
            if (loc1) 
            {
                this.removeMergeRechargeActive();
            }
            return;
        }

        private /* var */_mergeRecharge: IConsumeCountGiftBagModule;

        private /* var */_mergeRechargeIcon: MergeRechargeIcon;

        private /* var */_isGetCodes: Boolean=false;
    }
